package leetcode;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:逆波兰表达式求值
 * User: 东莞呵呵
 * Date:2022-07-14
 * Time:16:08
 */
public class TestDemo2 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if(isNumber(tokens[i])){
                stack.push(Integer.valueOf(tokens[i]));
            }else{
                int num1=stack.pop();
                int num2=stack.pop();
                int ret=0;
                switch (tokens[i]){
                    case "+":
                        ret=num1+num2;
                        break;
                    case "-":
                        ret=num2-num1;
                        break;
                    case "*":
                        ret=num2*num1;
                        break;
                    case "/":
                        ret=num2/num1;
                        break;
                    default:
                        System.out.println("输入有误");
                        break;
                }
                stack.push(ret);
            }
        }
        return stack.pop();
    }
    public static boolean isNumber(String s){
        if (!(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/"))){
            return true;
        }
        return false;
    }
}
